<?php
namespace Admin\Controller;
use Think\Controller;
use Think\Image;

class DesignController extends CommonController{
    //博文列表展示
    public function design(){
        $keywords=I('get.keywords');//搜索
        $table = M('design'); // 实例化User对象//数据表
        $count = $table->count();// 查询满足要求的总记录数
        $Page = new \Think\Page($count,8);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $Page->lastSuffix = false;
        $Page->setConfig('first','首页');
        $Page->setConfig('prev','上一页');
        $Page->setConfig('next','下一页');
        $Page->setConfig('last','尾页');
        $show = $Page->show();// 分页显示输出// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = $table->where("title like '%$keywords%'")->limit($Page->firstRow . ',' . $Page->listRows)->select();
        foreach ($list as $key=>$val){
            $list[$key]['title'] = str_replace ($keywords,'<font color="red">'.$keywords.'</font>',$val['title']);
        }
        $this->assign('keywords',$keywords); //搜索传
        $this->assign('list', $list);// 赋值数据集
        $this->assign('page', $show);// 赋值分页输出
        $this->display(); // 输出模板
    }

    //内容展示
    public function review(){
        $keywords=I('get.keywords');//搜索
        $table = M('design'); // 实例化User对象//数据表
        $count = $table->count();// 查询满足要求的总记录数
        $Page = new \Think\Page($count,99);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $Page->lastSuffix = false;
        $Page->setConfig('first','首页');
        $Page->setConfig('prev','上一页');
        $Page->setConfig('next','下一页');
        $Page->setConfig('last','尾页');
        $show = $Page->show();// 分页显示输出// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = $table->where("uname like '%$keywords%'")->limit($Page->firstRow . ',' . $Page->listRows)->select();
        foreach ($list as $key=>$val){
            $list[$key]['uname'] = str_replace ($keywords,'<font color="red">'.$keywords.'</font>',$val['uname']);
        }
        $this->assign('keywords',$keywords); //搜索传
        $this->assign('list', $list);// 赋值数据集
        $this->assign('page', $show);// 赋值分页输出
        $this->display(); // 输出模板
    }

//下拉菜单
    public function dodesign(){
        $user=M('insert');
        $list=$user->select();
        $this->assign("list",$list);
        $this->display();
    }


    //删除
    function delete(){
        $id = I('id');
        $res = M('design')->where("id=$id")->delete();  //数据表
        if ($res) {
            $this->success('成功');
        } else {
            $this->error('失败');
        }
    }
    //修改
//    public function todesign(){
//        $id=I('post.id');
//        $arrer=M('design')->where("id=$id")->find();
//        $this->assign('arrer',$arrer);
//        $this->display();
//    }
    //修改
    function todesign_do(){
        $id=I('post');
        $arrer=M('design')->delete($id);
        if ($arrer){
            $this->success('成功',U('Design/design'));
        }else{
            $this->error('失败');
        }
    }

    //添加
    public function dodoinsert(){
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     3145728 ;// 设置附件上传大小
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->savePath  =      './Public/Uploads/'; // 设置附件上传目录    // 上传文件
        $upload->rootPath  =        './'; //目录
        $info   =   $upload->upload();
        if(!$info) {// 上传错误提示错误信息
            $this->error($upload->getError());
        }
        $data = I ('post.');
        $table = M ('design');

        $data['img1']=$info['img1']['savepath'].$info['img1']['savename'];
        $res= $table->add($data);
        if ($res){
            $this->success('添加成功',U('Design/design'));
        }else{
            $this->error();
        }
    }

//    //点赞
//    public function praise(){
//        //接收文章ID和用户ID
//        $id = I('param.id');
//        $user_id = cookie('id');
//        //查看一条数据是否已经点赞过了
//        $list = M('dianzan')->where("text_id = '$id' and user_id = '$user_id'")->find();
//        if($list){
//            echo "no";die;
//        }
//        //点赞成功后添加一条数据
//        $re = M('dianzan')->data(array('text_id'=>$id,'user_id'=>$user_id))->add();
//        //点赞成功让文章点赞+1
//        $praise = M('design')->where("id = '$id'")->setInc('num',1);
//        //如果以上2条都满足
//        if($re && $praise){
//            //返回数据库的点赞数量
//            $number = M('design')->where("id = '$id'")->getField('num');
//            echo $number;
//        }else{
//            echo "error";
//        }
//    }

//    //评论显示
//    public function doreview(){
//        $data = I('post.');
//        $table = M('review');
//        $res = $table->add($data);
//        if ($res) {
//            $this->success('添加成功', U('Design/review'));
//        } else {
//            $this->error();
//        }
//    }

    //内容展示
    public function lists(){
       $id=I('param.id');
        $model =M('design');
        $res =$model
            ->where("id = $id")
            ->select();
        $this->assign('res',$res);
        $this->display('do_review');
    }



    public function do_review(){
        $num =  M('design')->count(); //获取评论总数
        $this->assign('num',$num);
        $data=$this->getCommlist();//获取评论列表
        $this->assign("commlist",$data);
        $this->display('do_review');
    }

    /**
     *添加评论
     */
    public function addComment(){
        $data=array();
        if((isset($_POST["comment"]))&&(!empty($_POST["comment"]))){
            $cm = json_decode($_POST["comment"],true);//通过第二个参数true，将json字符串转化为键值对数组
            $cm['create_time']=date('Y-m-d H:i:s',time());
            $cm['parent_id']=0;
            $cm['content']='游客';
            $cm['content']=$_POST["comment"];
            $newcm = M('design');
            $id = $newcm->add($cm);
            $cm["id"] = $id;
            $data = $cm;
            $num =  M('design')->count();//统计评论总数
            $data['num']= $num;
        }else{
            $data["error"] = "0";
        }
        $this->success('添加成功');
    }

    /**
     *添加评论
     */
    public function replyComment(){
        $id=$_GET["id"];
        $this->assign('id',$id);
        $this->display();
        $num =  M('design')->count();//统计评论总数
        $data['num']= $num;


        //$this->success('添加成功');
    }
    /**
     *递归获取评论列表
     */
    protected function getCommlist($parent_id = 0,&$result = array()){
        $arr = M('design')->where("parent_id = '".$parent_id."'")->order("create_time desc")->select();
        if(empty($arr)){
            return array();
        }
        foreach ($arr as $cm) {
            $thisArr=&$result[];
            $cm["children"] = $this->getCommlist($cm["id"],$thisArr);
            $thisArr = $cm;
        }
        return $result;
    }


}